package com.hyt.it.ogt.kq.service.admission.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import com.hyt.core.base.BaseEntity;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.cj.model.entity.ProjectCollectInfo;
import com.hyt.it.ogt.cj.model.entity.ProjectCollectInfoOption;
import com.hyt.it.ogt.kq.service.admission.IExamineeInfoExtService;
import com.hyt.it.ogt.kq.service.admission.IExamineeInfoService;
import com.hyt.it.ogt.kq.service.gov.mapper.admission.ExamineeInfoExtMapper;
import com.hyt.it.ogt.kq.service.model.entity.admission.ExamineeInfoExt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 准考证考生配置信息
 * </p>
 *
 * @author zhoumiao
 * @since 2023-01-10
 */
@Service
public class ExamineeInfoExtServiceImpl extends BaseServiceImpl<ExamineeInfoExtMapper, ExamineeInfoExt> implements IExamineeInfoExtService {

    @Autowired
    private IExamineeInfoService iExamineeInfoService;

    private static final Map<String, String> delimiterConvertMap = new HashMap<>();
    static {
        delimiterConvertMap.put(StrPool.COMMA, "/");
    }

    @Override
    public void deleteByIds(List<String> extIds) {
        lambdaUpdate()
                .in(BaseEntity::getId, extIds)
                .eq(BaseEntity::getDelFlag, false)
                .set(BaseEntity::getDelFlag, true)
                .update();
    }

    /**
     * 通过考生id和code删除
     *
     * @param deleteExtCodes
     * @param examineeId
     */
    @Override
    public void deleteByExamineeAndCodes(Set<String> deleteExtCodes, String examineeId) {
        if (CollectionUtil.isEmpty(deleteExtCodes)) {
            return;
        }
        lambdaUpdate()
                .eq(BaseEntity::getDelFlag, false)
                .in(ExamineeInfoExt::getVariableCode, deleteExtCodes)
                .eq(ExamineeInfoExt::getExamineeId, examineeId)
                .set(BaseEntity::getDelFlag, true)
                .update();
    }

    /**
     * 获取配置项的具体值 如果配置项有option就通过用户选择的value找到对应option中对应label值，
     * 没有option则使用户填写的value值
     *
     * @param infoValue
     * @param projectCollectInfoOptions
     * @param projectCollectInfo
     * @return
     */
    @Override
    public String getVariableValue(String infoValue, Map<String, List<ProjectCollectInfoOption>> projectCollectInfoOptions, ProjectCollectInfo projectCollectInfo) {
        String variableValue = "";
        if (projectCollectInfoOptions.containsKey(projectCollectInfo.getId())) {
            // 有子选项 具体的值取子选项中的label
            String split = StrPool.COMMA;
            List<ProjectCollectInfoOption> projectCollectOptionMapOrDefault = projectCollectInfoOptions.getOrDefault(projectCollectInfo.getId(), Collections.emptyList());
            if (StrUtil.contains(infoValue, split)) {
                // 有逗号分割的涉及到多个label
                String[] vs = infoValue.split(split);
                StringJoiner sb = new StringJoiner(delimiterConvertMap.get(split));
                for (String v : vs) {
                    Optional<ProjectCollectInfoOption> first = projectCollectOptionMapOrDefault.stream().filter(e -> Objects.equals(v, e.getOptionValue())).findFirst();
                    first.ifPresent(projectCollectInfoOption -> sb.add(projectCollectInfoOption.getOptionLabel()));
                }
                variableValue = sb.toString();
            } else {
                Optional<ProjectCollectInfoOption> first = projectCollectOptionMapOrDefault.stream().filter(e -> Objects.equals(infoValue, e.getOptionValue())).findFirst();
                if (first.isPresent()) {
                    variableValue = first.get().getOptionLabel();
                }
            }
        } else {
            variableValue = infoValue;
        }
        return variableValue;
    }

    /**
     * 通过准考证考生id查询
     *
     * @param examineeId
     * @return
     */
    @Override
    public List<ExamineeInfoExt> listByExaminee(String examineeId) {
        return lambdaQuery().eq(BaseEntity::getDelFlag, false).in(ExamineeInfoExt::getExamineeId, examineeId).list();
    }

    /**
     * 通过准考证号id和code码更新value
     *
     * @param updateExamineeInfoExt
     * @param examineeId
     * @param currentUser
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateValueByExamineeAndCode(List<ExamineeInfoExt> updateExamineeInfoExt, String examineeId, String currentUser) {
        for (ExamineeInfoExt examineeInfoExt : updateExamineeInfoExt) {
            lambdaUpdate()
                    .eq(BaseEntity::getDelFlag, false)
                    .eq(ExamineeInfoExt::getExamineeId, examineeId)
                    .eq(ExamineeInfoExt::getVariableCode, examineeInfoExt.getVariableCode())
                    .set(ExamineeInfoExt::getVariableValue, examineeInfoExt.getVariableValue())
                    .set(ExamineeInfoExt::getUpdateBy, currentUser)
                    .set(ExamineeInfoExt::getUpdateDate, LocalDateTime.now())
                    .update();
        }
    }
}
